package com.hsurosy.domain.credit.service.adjust;

import com.hsurosy.domain.credit.event.CreditAdjustSuccessMessageEvent;
import com.hsurosy.domain.credit.model.aggregate.TradeAggregate;
import com.hsurosy.domain.credit.model.entity.CreditAccountEntity;
import com.hsurosy.domain.credit.model.entity.CreditOrderEntity;
import com.hsurosy.domain.credit.model.entity.TaskEntity;
import com.hsurosy.domain.credit.model.entity.TradeEntity;
import com.hsurosy.domain.credit.repository.ICreditRepository;
import com.hsurosy.domain.credit.service.ICreditAdjustService;
import com.hsurosy.types.event.BaseEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author Hsu琛君珩
 * @Date 2024-11-25 13:07
 * @Description 积分调额服务
 * @Version: v1.0.0
 */
@Slf4j
@Service
public class CreditAdjustService implements ICreditAdjustService {

    @Resource
    private ICreditRepository creditRepository; // 积分存储库接口
    @Resource
    private CreditAdjustSuccessMessageEvent creditAdjustSuccessMessageEvent; // 积分调整成功的消息事件

    /**
     * 创建积分调整订单
     *
     * @param tradeEntity 包含积分调整相关信息的实体对象
     * @return 返回创建的订单ID
     */
    @Override
    public String createOrder(TradeEntity tradeEntity) {
        log.info("账户积分额度开始 userId: {} tradeName: {} amount: {}", tradeEntity.getUserId(), tradeEntity.getTradeName(), tradeEntity.getAmount());
        // 1. 创建账户积分实体，记录账户的变动信息
        CreditAccountEntity creditAccountEntity = TradeAggregate.createCreditAccountEntity(
                tradeEntity.getUserId(),
                tradeEntity.getAmount());

        // 2. 创建账户订单实体，生成订单记录
        CreditOrderEntity creditOrderEntity = TradeAggregate.createCreditOrderEntity(
                tradeEntity.getUserId(),
                tradeEntity.getTradeName(),
                tradeEntity.getTradeType(),
                tradeEntity.getAmount(),
                tradeEntity.getOutBusinessNo());

        // 3. 构建消息任务对象
        CreditAdjustSuccessMessageEvent.CreditAdjustSuccessMessage creditAdjustSuccessMessage = new CreditAdjustSuccessMessageEvent.CreditAdjustSuccessMessage();
        creditAdjustSuccessMessage.setUserId(tradeEntity.getUserId());
        creditAdjustSuccessMessage.setOrderId(creditOrderEntity.getOrderId());
        creditAdjustSuccessMessage.setAmount(tradeEntity.getAmount());
        creditAdjustSuccessMessage.setOutBusinessNo(tradeEntity.getOutBusinessNo());
        // 构建事件消息，用于异步通知积分调整结果
        BaseEvent.EventMessage<CreditAdjustSuccessMessageEvent.CreditAdjustSuccessMessage> creditAdjustSuccessMessageEventMessage = creditAdjustSuccessMessageEvent.buildEventMessage(creditAdjustSuccessMessage);

        // 构建任务实体，管理消息任务
        TaskEntity taskEntity = TradeAggregate.createTaskEntity(tradeEntity.getUserId(), creditAdjustSuccessMessageEvent.topic(), creditAdjustSuccessMessageEventMessage.getId(), creditAdjustSuccessMessageEventMessage);

        // 4. 构建交易聚合对象，将账户记录和订单记录绑定在一起
        TradeAggregate tradeAggregate = TradeAggregate.builder()
                .userId(tradeEntity.getUserId())
                .creditAccountEntity(creditAccountEntity)
                .creditOrderEntity(creditOrderEntity)
                .taskEntity(taskEntity)
                .build();

        // 5. 保存交易聚合对象到数据库
        creditRepository.saveUserCreditTradeOrder(tradeAggregate);
        log.info("账户积分额度完成 userId: {} orderId: {}", tradeEntity.getUserId(), creditOrderEntity.getOrderId());

        // 6. 返回生成的订单ID
        return creditOrderEntity.getOrderId();
    }

    /**
     * 查询用户积分账户信息
     *
     * @param userId 用户ID，用于标识查询的目标用户
     * @return 包含用户积分账户信息的实体对象，包括总积分、可用积分等字段
     */
    @Override
    public CreditAccountEntity queryUserCreditAccount(String userId) {
        return creditRepository.queryUserCreditAccount(userId);
    }

}
